

import XlsxData from "../config/XlsxData";
import BaseLangManager from "./BaseLangManager";
import { LangLabel } from "./LangLabel";
const i18n = require('LanguageData');
export enum LangOptionType{
    VALUE,
    LANG_ID,
    
}
export  class LangOption {
    private type:LangOptionType = LangOptionType.VALUE;
    private value: any;  
    
    constructor(type:LangOptionType,value){
        this.type = type;
        this.value = value;
    }


    getType(){
        return this.type;
    }
    getValue(){
        return this.value;
    }

}

export  class LangLabelManager extends BaseLangManager {
    private data:XlsxData;
    clear():void{
        this.data = null;
    }
    setLang(langName:string,data:XlsxData){
        if(this.langName == langName){
            return;
        }
        this.data = data;
        // cc.log(' langName ',langName)
        if(i18n){
            i18n.init(langName);
        }
        
        this.langName  = langName;
    }    

    getValue(langID:string,opt?){
       
        if(!this.data) {
            cc.warn("LangManager is not init localString langID  ",langID,this.data);
            return langID;
        }
        
        let str:string = this.data.getValue(langID);
        if(!str){
            if(i18n){
                str = i18n.t(langID)
            }
           
        }
        if (str) {
            if (str.indexOf('\\')>=0) {
                str = str.replace(/\\n/g, '\n');
            }
            if(opt){
                for (const key in opt) {
                    // let option:LangOption = opt[key];
                    let value = opt[key];
                    // if(option.getType() == LangOptionType.LANG_ID){
                    //     value = this.data.getValue(value);
                    // }
                    str = str.replace("%{"+key+"}", value);
                }
            }
        } else {
            str = langID;
        }
        return str;
    }

    update(){
        let rootNodes = cc.director.getScene().children;
        let allLocalizedLabels = [];
        for (let i = 0; i < rootNodes.length; ++i) {

            let labels = rootNodes[i].getComponentsInChildren(LangLabel);
            Array.prototype.push.apply(allLocalizedLabels, labels);
        }
        for (let i = 0; i < allLocalizedLabels.length; ++i) {
            let label = allLocalizedLabels[i];
            label.updateValue();
        }
    }

}
